package com.yuntongxun.ecdemo.voicechat.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.voicechat.bean.PostResult;
import com.yuntongxun.ecdemo.voicechat.bean.UserInfo;
import com.yuntongxun.ecdemo.voicechat.comman.Contants;
import com.yuntongxun.ecdemo.voicechat.comman.EventBusTags;
import com.yuntongxun.ecdemo.voicechat.ui.activity.CollectionActivity;
import com.yuntongxun.ecdemo.voicechat.ui.activity.GiftsActivity;
import com.yuntongxun.ecdemo.voicechat.ui.activity.InfoActivity;
import com.yuntongxun.ecdemo.voicechat.ui.activity.MyPartnersActivity;
import com.yuntongxun.ecdemo.voicechat.ui.activity.SettingActivity;
import com.yuntongxun.ecdemo.voicechat.ui.activity.WalletActivity;
import com.yuntongxun.ecdemo.voicechat.util.InfoUtil;
import com.yuntongxun.ecdemo.voicechat.util.JsonHelper;
import com.yuntongxun.ecdemo.voicechat.util.PreferenceUtils;

import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 我的
 *
 * @author li_zhuonan
 * @version 1.0
 * @since 2016/11/18
 */

public class MeFragment extends BaseVoiceChatFragment {


    @InjectView(R.id.toolbar_title)
    TextView toolbarTitle;
    @InjectView(R.id.toolbar_back_btn)
    ImageButton toolbarBackBtn;
    @InjectView(R.id.name)
    TextView nameTv;
//    @InjectView(R.id.gendeImg)
//    ImageView gendeImg;
//    @InjectView(R.id.ageTv2)
//    TextView ageTv;
//    @InjectView(R.id.countryImg)
//    ImageView countryImg;
//    @InjectView(R.id.languageTv)
//    TextView languageTv;
    @InjectView(R.id.countTv)
    TextView countTv;


    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected void onUserInvisible() {

    }
    UserInfo userInfo;
    @Override
    protected void initViewsAndEvents() {
        toolbarBackBtn.setVisibility(View.GONE);
        toolbarTitle.setText("Me");
        initData();

    }

    private void initData(){
        String data=PreferenceUtils.getPrefString(getActivity(), Contants.Preference.user_info,"");
        if (!data.equals("")){
            JsonHelper<UserInfo> jsonHelper = new JsonHelper<UserInfo>(UserInfo.class);
            userInfo = jsonHelper.getData(data, "result");
            bindData(userInfo);
        }
    }

    private void bindData(UserInfo userInfo ){
        InfoUtil.setNickName(userInfo,getActivity(),nameTv);
//        nameTv.setText(userInfo.getNickName());
//        ageTv.setText(userInfo.getAge() + "");
//        try {
//            InputStream is=getActivity().getAssets().open("flag/"+userInfo.getCountry()+".png");
//            countryImg.setImageBitmap(BitmapFactory.decodeStream(is));
//            is.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        languageTv.setText(userInfo.getNativeLanguage()+"/"+userInfo.getLearningLanguage());
        countTv.setText(userInfo.getAccountName());
    }

    @OnClick({R.id.info_layout,R.id.my_partners_layout,R.id.my_favorites_layout,R.id.my_wallet_layout,R.id.my_gifts_layout,R.id.my_setting_layout})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.info_layout:
                Intent intent = new Intent(getActivity(), InfoActivity.class);
                intent.putExtra(InfoActivity.FROM, InfoActivity.MYSELF);
                startActivity(intent);
                break;
            case R.id.my_partners_layout:
                intent=new Intent(getActivity(), MyPartnersActivity.class);
                intent.putExtra("account",userInfo.getAccountName());
                startActivity(intent);
                break;
            case R.id.my_favorites_layout:
                startActivity(new Intent(getActivity(), CollectionActivity.class));
                break;
            case R.id.my_wallet_layout:
                startActivity(new Intent(getActivity(), WalletActivity.class));
                break;
            case R.id.my_gifts_layout:
                startActivity(new Intent(getActivity(), GiftsActivity.class));
                break;
            case R.id.my_setting_layout:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
        }
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_me;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }

    public void onEvent(PostResult postResult){
        if (EventBusTags.UPDATE_ME.equals(postResult.getTag())){
            initData();

        }
    }


}
